package com.vdf.sisconsolid.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.vdf.sisconsolid.persistencemodule.dao.AddressDao;
import com.vdf.sisconsolid.persistencemodule.dto.KeyValueBean;
import com.vdf.sisconsolid.rest.requestresponsedata.FilterParameter;
import com.vdf.sisconsolid.rest.requestresponsedata.ReqRespData;
import com.vdf.sisconsolid.rest.requestresponsedata.RequestAndRespondHandler;
import com.vdf.sisconsolid.util.CacheSC;

public class AddressController {

	private static Logger log = Logger.getLogger(AddressController.class);

	public static Map<String, Object> processGetProvinces() {

		log.info("Into processGetProvinces");
		List<KeyValueBean> provinceBeanList = CacheSC.getProvinceList();

		log.info("provinceBeanList size: " + provinceBeanList.size());
		return RequestAndRespondHandler.getResponseRest(true,
				ReqRespData.data.toString(), "Loaded provinces",
				provinceBeanList);
	}

	public static Map<String, Object> processGetCities(String jsonFilters) {

		log.info("Into processGetCities, filters:" + jsonFilters);

		FilterParameter[] filters = (FilterParameter[]) RequestAndRespondHandler
				.fromJson(jsonFilters, FilterParameter[].class);

		boolean success = true;
		List<KeyValueBean> cityBeanList = new ArrayList<KeyValueBean>();
		if (filters.length == 1 && filters[0].getValue() != null) {
			Map<String, Object> parameters = new HashMap<String, Object>();
			Integer provinceId = (Integer) filters[0].getValue();

			parameters.put(filters[0].getProperty(), provinceId.longValue());

			cityBeanList = CacheSC.getCityList(provinceId, parameters);

		} else {
			success = false;
		}
		log.info("cityBeanList size: " + cityBeanList.size());
		return RequestAndRespondHandler.getResponseRest(success,
				ReqRespData.data.toString(), "Loaded cities ", cityBeanList);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> processGetNeighborhoods(
			String jsonFilters, HttpServletRequest request) {
		log.info("Into processGetNeighborhoods, filters: " + jsonFilters);
		FilterParameter[] filters = (FilterParameter[]) RequestAndRespondHandler
				.fromJson(jsonFilters, FilterParameter[].class);
		List<KeyValueBean> neighborhoodBeanList =  new ArrayList<KeyValueBean>();
		if (filters.length == 1 && filters[0].getValue() != null) {
			Map<String, Object> parameters = new HashMap<String, Object>();
			Integer cityId = (Integer) filters[0].getValue();

			neighborhoodBeanList = (List<KeyValueBean>) request.getSession()
					.getAttribute(cityId + "_neighborhoodBeanList");

			if (neighborhoodBeanList == null) {
				parameters.put(filters[0].getProperty(), cityId.longValue());
				neighborhoodBeanList = new AddressDao()
						.getNeighborhoodList(parameters);
				request.getSession().setAttribute(
						cityId + "_neighborhoodBeanList", neighborhoodBeanList);
			}
		}
		log.info("neighborhoodBeanList size: " + neighborhoodBeanList.size());
		return RequestAndRespondHandler.getResponseRest(true,
				ReqRespData.data.toString(), "Loaded neighborhoods",
				neighborhoodBeanList);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> processGetStreets(String jsonFilters,
			HttpServletRequest request) {
		log.info("Into processGetStreets, filters: " + jsonFilters);
		FilterParameter[] filters = (FilterParameter[]) RequestAndRespondHandler
				.fromJson(jsonFilters, FilterParameter[].class);
		List<KeyValueBean> streetBeanList =  new ArrayList<KeyValueBean>();
		if (filters.length == 1 && filters[0].getValue() != null) {
			Map<String, Object> parameters = new HashMap<String, Object>();
			Integer cityId = (Integer) filters[0].getValue();

			streetBeanList = (List<KeyValueBean>) request.getSession()
					.getAttribute(cityId + "_streetBeanList");

			if (streetBeanList == null) {
				parameters.put(filters[0].getProperty(), cityId.longValue());
				streetBeanList = new AddressDao().getStreetList(parameters);
				request.getSession().setAttribute(cityId + "_streetBeanList",
						streetBeanList);
			}
		}
		log.info("streetBeanList size: " + streetBeanList.size());
		return RequestAndRespondHandler.getResponseRest(true,
				ReqRespData.data.toString(), "Loaded streets ", streetBeanList);
	}

}
